package cn.wolfcode.trip.app.controller;


import cn.wolfcode.trip.base.domain.PrivateMessage;
import cn.wolfcode.trip.base.domain.SystemMessage;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.PrivateMessageQueryObject;
import cn.wolfcode.trip.base.service.IPrivateMessageService;
import cn.wolfcode.trip.base.service.ISystemMessageService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息资源
 * @author CHN
 */
@RestController
@RequestMapping("messages")
public class MessageController {
    @Autowired
    private IPrivateMessageService privateMessageService;
    @Autowired
    private ISystemMessageService systemMessageService;

    /**
     * 根据收信人id查询私信,用于聊天窗口回显
     * @param qo
     * @return
     * @author CHN
     */
    @GetMapping("{receiverId}/privateMessages")
    public PageInfo privateMessage(PrivateMessageQueryObject qo){
        //不翻页
        qo.setPageSize(0);
        //设置查询为已读的的
        qo.setBrower(1);
        return privateMessageService.query(qo);
    }

    /**
     * 询轮 查询是否有消息 在聊天的时候
     * @param qo
     * @return
     * @author CHN
     */
    @GetMapping
    public PageInfo selectMessage(PrivateMessageQueryObject qo){
        //不翻页
        qo.setPageSize(0);
        return privateMessageService.query(qo);
    }



    /**
     * 查询私信列表好友
     * @return 收信人集合/查未读信息
     * @author CHN
     */
    @GetMapping("{senderId}")
    public List<User> getReceiver(PrivateMessageQueryObject qo){
        List<User> receiver = privateMessageService.getReceiver(qo);
        return  receiver;
    };




    /**
     * 信息新增
     * @return
     * @author CHN
     */
    @PostMapping
    public JSONResult save(PrivateMessage privateMessage) {
        privateMessageService.save(privateMessage);
        return new JSONResult();
    }


    /**
     * 根据id修改私信的状态为已读
     * @param id
     * @return
     * @author CHN
     *
     */
    @PutMapping("{id}")
    public JSONResult updateBrower(@PathVariable("id") Long id){
        privateMessageService.updateBrower(id);
        return new JSONResult();
    }

    /**
     * 根据发送人senderId修改私信的状态为已读
     * @param senderId
     * @return
     */
    @PutMapping("updateBySenderId/{senderId}")
    public JSONResult updateBySenderId(@PathVariable("senderId") Long senderId){
        privateMessageService.updateBySenderId(senderId);
        return new JSONResult();
    }


    /**
     * 查看系统消息
     * @param qo
     * @return
     * @author CHN
     */
    @GetMapping("systemMessage")
    public PageInfo selectSystemMessage(SystemMessageQueryObject qo){
        return systemMessageService.query(qo);
    }


    @PostMapping("systemMessage")
    public JSONResult save(SystemMessage systemMessage){
        systemMessageService.save(systemMessage);
        return new JSONResult();
    }


    /**
     * 根据id修改系统信息的状态为已读
     * @param id
     * @return
     */
    @PutMapping("systemMessage/{id}")
    public JSONResult updateSystemMessageBrower(@PathVariable("id") Long id){
        systemMessageService.updateBrower(id);
        return new JSONResult();
    }

}




